﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.ManagerPage
{
    public partial class Reports : System.Web.UI.Page
    {
        LoginDetails details;
        bool isAdmin;
        protected void Page_Load(object sender, EventArgs e)
        {
            details = (LoginDetails)Session["managerDetails"];
            if (Session["myobject"] != null)
            {
                isAdmin = ((LoginDetails)Session["myobject"]).isAdmin;
            }
            try
            {
                if (details == null || !details.isManager)
                {
                    //hack attempt
                    Response.Redirect("~/General/unAuthorized.aspx");
                }
            }
            catch
            {
                //hack attempt
                Response.Redirect("~/General/unAuthorized.aspx");
            }
            
            //post feedback
            this.eMsgText.Text = null;
            if (Session["errMsg"] != null)
            {
                this.eMsgText.Text = (string)Session["errMsg"];
                this.eMsgText.Visible = true;
                Session["errMsg"] = null;
            }

            initLogoutButton();
        }

        private void initLogoutButton()
        {
            //experimental logout feature
            Control h = this.Header.FindControl("LoginView1");
            Control h2 = h.FindControl("loginLink");
            ((System.Web.UI.HtmlControls.HtmlAnchor)h2).HRef = "./Account/Login.aspx";
            ((System.Web.UI.HtmlControls.HtmlAnchor)h2).InnerText = "Log Out";
        }

        protected void MonthlyByID(object sender, EventArgs e)
        {
            clearButtons();
            Table1.Controls.Clear();
            Label2.Text = "Enter an Employee ID";
            Label2.Visible = true;
            EmpNum.Visible = true;
            byEmpButton.Visible = true;
        }

        protected void MonthlyByIDGoClicked(object sender, EventArgs e)
        {
            if (EmpNum.Text == "" || EmpNum.Text == "null")
            {
                RequiredFieldValidator1.Visible = true;
                return;
            }
            MonthlyByIDFunc(EmpNum.Text,false);
            Label2.Visible = false;
            EmpNum.Visible = false;
            byEmpButton.Visible = false;
        }

        protected void didntFinishMonth(object sender, EventArgs e)
        {
            Table1.Controls.Clear();
            Table1.Visible = true;

            List<Employee> list = null;
            try
            {
                if (!isAdmin)
                {
                    list = BL.getAllEmployeesByTeam(BL.getEmployeeById(details.ID).managerOf,false);
                }
                else
                {
                    list = BL.getAllEmployeesByTeam("",true);
                }
            }
            catch
            {
                Session["errMsg"] = "You Manage no one.../connection went wrong";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            if (list == null)
            {
                Session["errMsg"] = "You Manage no one.../connection went wrong";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            //table header
            TableRow header = new TableRow();
            TableCell one = new TableCell();
            one.Text = "ID";
            TableCell two = new TableCell();
            two.Text = "needs";
            TableCell three = new TableCell();
            three.Text = "did";
            header.Cells.Add(one); header.Cells.Add(two); header.Cells.Add(three);
            Table2.Rows.Add(header);

            foreach (Employee emp in list)
            {
                minHoursAndEmp m = MonthlyByIDFunc(emp.id, true);
                TableCell a = new TableCell();
                a.Text = emp.id;
                TableCell b = new TableCell();
                b.Text = emp.minHours;
                TableCell c = new TableCell();
                c.Text = m.totHours.ToString();
                TableRow d = new TableRow();
                d.Cells.Add(a); d.Cells.Add(b); d.Cells.Add(c);
                Table2.Rows.Add(d);
            }

            Table2.Visible = true;

        }

        private minHoursAndEmp MonthlyByIDFunc(string empId, Boolean duties)
        {
            LinkedList<Shift> linkedList = null;
            try
            {
                linkedList = BL.getAllShiftsById(empId);
            }
            catch
            {
                Session["errMsg"] = "No Such Employee/connection went wrong";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }


            Table1.Visible = true;

            //add header to table
            TableRow header = new TableRow();
            TableCell n = new TableCell();
            n.Text = "ID";
            TableCell a = new TableCell();
            a.Text = "Day";
            TableCell h = new TableCell();
            h.Text = "Amount";
            header.Cells.Add(n); header.Cells.Add(a); header.Cells.Add(h);
            Table1.Rows.Add(header);



            int daysCounter = 0;
            int totHours = 0;
            while (daysCounter < 31)
            {
                daysCounter++;
                DateTime zero = new DateTime(DateTime.Now.Year, DateTime.Now.Month, daysCounter, 0, 0, 0);
                
                if (linkedList.Count != 0)
                {
                    foreach (Shift s in linkedList)
                    {
                        //somthing to sum up time in
                        if (int.Parse(s.getStartMonth()).Equals(zero.Month))
                        {
                            if (int.Parse(s.getStartDay()).Equals(zero.Day))
                            {
                                if (s.getEndHour() != null && s.getStartHour() != null)
                                {
                                    int hoursToAdd = (int.Parse(s.getEndHour()) - int.Parse(s.getStartHour()));
                                    int minsToAdd = (60 - int.Parse(s.getStartMin())) + int.Parse(s.getEndMin());
                                    zero = zero.AddHours(hoursToAdd);
                                    zero = zero.AddMinutes(minsToAdd);
                                }
                            }
                        }
                    }
                }

                totHours += zero.Hour;
                //add results to table
                TableRow row = new TableRow();
                TableCell id = new TableCell();
                id.Text = empId;
                TableCell day = new TableCell();
                day.Text = zero.Day.ToString() + "/" + zero.Month.ToString() + "/" + zero.Year.ToString();
                TableCell amount = new TableCell();
                amount.Text = zero.Hour.ToString() + ":" + zero.Minute.ToString();
                row.Cells.Add(id); row.Cells.Add(day); row.Cells.Add(amount);
                Table1.Rows.Add(row);
            
            }

            if (duties)
            {
                Table1.Controls.Clear();
                return new minHoursAndEmp(empId, totHours);
            }

            //add footer to table
            TableRow footer = new TableRow();
            TableCell totHoursText = new TableCell();
            totHoursText.Text = "Total (Estimated..) : " + totHours.ToString();
            totHoursText.ForeColor = System.Drawing.Color.Red;
            footer.Cells.Add(totHoursText);
            Table1.Rows.Add(footer);

            clearButtons();
            return null;
        }

        protected void viewChangeLog(object sender, EventArgs e)
        {
            clearButtons();
            byIDButton.Visible = true;
            byTeamButton.Visible = true;
            if (isAdmin)
            {
                entireChangeLog.Visible = true;
            }
            cl.Visible = false;
        }

        private void clearButtons()
        {
            byIDButton.Visible = false;
            byTeamButton.Visible = false;
            entireChangeLog.Visible = false;
            cl.Visible = false;
            byIDButton.CommandName="";
            byTeamButton.CommandName="";
            entireChangeLog.CommandName = "";
            clLabel.Visible = false;
            EmpNum.Text = "";
            EmpNum.Visible = false;
        }

        protected void viewChangeLogID(object sender, EventArgs e)
        {
            clearButtons();
            EmpNum.Visible = true;
            clLabel.Text = "Enter Employee ID";
            cl.Visible = true;
            byIDButton.CommandName = "clicked";
        }

        protected void viewChangeLogTeam(object sender, EventArgs e)
        {
            clearButtons();
            EmpNum.Visible = true;
            clLabel.Text = "Enter Team Name";
            cl.Visible = true;
            byTeamButton.CommandName = "clicked";
        }

        protected void changeLogSelector(object sender, EventArgs e)
        {
            if (byIDButton.CommandName == "clicked")
            {
                viewChangeLogIDMaker();
                clearButtons();
            }
            else if (byTeamButton.CommandName == "clicked")
            {
                viewChangeLogTeamMaker();
                clearButtons();
            }
            else
            {
                //should never get here....
                Session["errMsg"] = "this is some weird shit...";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }
        }

        private void viewChangeLogIDMaker()
        {
            Table1.Controls.Clear();

            List<ChangeLogEntry> list = null;
            try
            {
                list = BL.getChangeLogForID(EmpNum.Text);
            }
            catch
            {
                Session["errMsg"] = "connection went wrong, please try again later";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            if (list == null)
            {
                Session["errMsg"] = "No Entries in the changelog!";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            Table1.Visible = true;
            Table1.Width = 1024;

            //add header to table
            TableRow header = new TableRow();
            TableCell a = new TableCell();
            a.Width = 100;
            a.Text = "ChangeLog ID";
            TableCell b = new TableCell();
            b.Width = 100;
            b.Text = "Employee ID";
            TableCell d = new TableCell();
            d.Width = 100;
            d.Text = "Team";
            TableCell f = new TableCell();
            f.Width = 700;
            f.Text = "Event Description";
            header.Cells.Add(a); header.Cells.Add(b); header.Cells.Add(d); header.Cells.Add(f);
            Table1.Rows.Add(header);

            foreach (ChangeLogEntry c in list)
            {
                TableRow row = new TableRow();
                TableCell one = new TableCell();
                one.Text = c.changeLogId.ToString();
                one.Width = 100;
                TableCell two = new TableCell();
                two.Text = c.ID;
                two.Width = 100;
                two.ForeColor = System.Drawing.Color.Magenta;
                TableCell three = new TableCell();
                three.Text = c.team;
                three.Width = 100;
                TableCell four = new TableCell();
                four.Text = c.text;
                four.Width = 700;
                four.ForeColor = System.Drawing.Color.Red;
                row.Cells.Add(one); row.Cells.Add(two); row.Cells.Add(three); row.Cells.Add(four);
                Table1.Rows.Add(row);
            }
            clearButtons();
        }

        private void viewChangeLogTeamMaker()
        {
            Table1.Controls.Clear();

            List<ChangeLogEntry> list = null;
            try
            {
                list = BL.getChangeLogForTeam(EmpNum.Text);
            }
            catch
            {
                Session["errMsg"] = "connection went wrong, please try again later";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            if (list == null)
            {
                Session["errMsg"] = "No Entries in the changelog!";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            Table1.Visible = true;
            Table1.Width = 1024;

            //add header to table
            TableRow header = new TableRow();
            TableCell a = new TableCell();
            a.Width = 100;
            a.Text = "ChangeLog ID";
            TableCell b = new TableCell();
            b.Width = 100;
            b.Text = "Employee ID";
            TableCell d = new TableCell();
            d.Width = 100;
            d.Text = "Team";
            TableCell f = new TableCell();
            f.Width = 700;
            f.Text = "Event Description";
            header.Cells.Add(a); header.Cells.Add(b); header.Cells.Add(d); header.Cells.Add(f);
            Table1.Rows.Add(header);

            foreach (ChangeLogEntry c in list)
            {
                TableRow row = new TableRow();
                TableCell one = new TableCell();
                one.Text = c.changeLogId.ToString();
                one.Width = 100;
                TableCell two = new TableCell();
                two.Text = c.ID;
                two.Width = 100;
                two.ForeColor = System.Drawing.Color.Magenta;
                TableCell three = new TableCell();
                three.Text = c.team;
                three.Width = 100;
                TableCell four = new TableCell();
                four.Text = c.text;
                four.Width = 700;
                four.ForeColor = System.Drawing.Color.Red;
                row.Cells.Add(one); row.Cells.Add(two); row.Cells.Add(three); row.Cells.Add(four);
                Table1.Rows.Add(row);
            }
            clearButtons();
        }

        protected void viewEntireChangeLog(object sender, EventArgs e)
        {
            Table1.Controls.Clear();

            List<ChangeLogEntry> list = null;
            try
            {
                list = BL.getChangeLogForAll();
            }
            catch
            {
                Session["errMsg"] = "connection went wrong, please try again later";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            if (list == null)
            {
                Session["errMsg"] = "No Entries in the changelog!";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            Table1.Visible = true;
            Table1.Width = 1024;

            //add header to table
            TableRow header = new TableRow();
            TableCell a = new TableCell();
            a.Width = 100;
            a.Text = "ChangeLog ID";
            TableCell b = new TableCell();
            b.Width = 100;
            b.Text = "Employee ID";
            TableCell d = new TableCell();
            d.Width = 100;
            d.Text = "Team";
            TableCell f = new TableCell();
            f.Width = 700;
            f.Text = "Event Description";
            header.Cells.Add(a); header.Cells.Add(b); header.Cells.Add(d); header.Cells.Add(f);
            Table1.Rows.Add(header);

            foreach (ChangeLogEntry c in list)
            {
                TableRow row = new TableRow();
                TableCell one = new TableCell();
                one.Text = c.changeLogId.ToString();
                one.Width = 100;
                TableCell two = new TableCell();
                two.Text = c.ID;
                two.Width = 100;
                two.ForeColor = System.Drawing.Color.Magenta;
                TableCell three = new TableCell();
                three.Text = c.team;
                three.Width = 100;
                TableCell four = new TableCell();
                four.Text = c.text;
                four.Width = 700;
                four.ForeColor = System.Drawing.Color.Red;
                row.Cells.Add(one); row.Cells.Add(two); row.Cells.Add(three); row.Cells.Add(four);
                Table1.Rows.Add(row);
            }
            clearButtons();
        }


        protected void MonthlyForAll(object sender, EventArgs e)
        {
            Table1.Controls.Clear();
            List<Employee> list = null;
            try
            {
                if(!isAdmin)
                    list = BL.getAllEmployeesByTeam(BL.getEmployeeById(details.ID).managerOf,false);
                else
                    list = BL.getAllEmployeesByTeam("", true);
            }
            catch
            {
                Session["errMsg"] = "mfa connection went wrong";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            if (list == null)
            {
                Session["errMsg"] = "mfa connection went wrong";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            foreach (Employee emp in list)
            {
                MonthlyByIDFunc(emp.id,false);
            }
        }

        protected void whoAtWorkReport(object sender, EventArgs e)
        {
            Table1.Controls.Clear();
            Table1.Visible = true;
            List<Employee> list = null;
            try
            {
                if (!isAdmin)
                    list = BL.getAllEmployeesByTeam(BL.getEmployeeByLoginDetails(details).Team,false);
                else
                {
                    list = BL.getAllEmployeesByTeam("",true);
                }
            }
            catch
            {
                //no emps to manage!
                Session["errMsg"] = "You Have No Employees to Manage!";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            if (list == null)
            {
                //no emps to manage!
                Session["errMsg"] = "You Have No Employees to Manage!";
                Response.Redirect("~/ManagerPage/Reports.aspx");
            }

            TableRow header = new TableRow();
            TableCell n = new TableCell();
            n.Text = "Name";
            TableCell a = new TableCell();
            a.Text = "At Work?";
            TableCell h = new TableCell();
            h.Text = "How Long?";
            header.Cells.Add(n); header.Cells.Add(a); header.Cells.Add(h);
            Table1.Rows.Add(header);

            foreach (Employee emp in list)
            {
                LoginDetails empDetails = new LoginDetails(emp.id, false, false);
                TableRow row = new TableRow();
                
                //name
                TableCell name = new TableCell();
                name.Text = emp.firstName + " " + emp.lastName;
                row.Cells.Add(name);

                //at work?
                TableCell atWork = new TableCell();
                atWork.Text = empDetails.checkedIntoWork.ToString();
                row.Cells.Add(atWork);
                
                //how many hours?
                TableCell hours = new TableCell();
                string lastOpen = null;
                try
                {
                    lastOpen = BL.getLastOpenShift(empDetails.ID);
                }
                catch { }
                if(lastOpen!=null)
                    hours.Text = (DateTime.Now.Hour - (int.Parse(lastOpen.Split(new string[] { "::" }, StringSplitOptions.None)[1].Split(':')[0]))).ToString()
                            + " Hours";

                row.Cells.Add(hours);

                //add the row
                Table1.Rows.Add(row);
            }
        }
    }

    public class minHoursAndEmp
    {
        public String emp;
        public int totHours;

        public minHoursAndEmp(String emp, int totHours)
        {
            this.emp = emp;
            this.totHours = totHours;
        }
    }
}